package m202406.d25;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/26 14:56
 */
public class LCR075 {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr2.length; i++) {
            map.put(arr2[i], i + 1);
        }

        return Arrays.stream(arr1)
                .boxed()
                .sorted((x, y) -> {
                    if (map.containsKey(x) && map.containsKey(y)) {
                        return map.get(x) - map.get(y);
                    } else if (map.containsKey(x)) {
                        return -1;
                    } else if (map.containsKey(y)) {
                        return 1;
                    } else {
                        return x - y;
                    }
                })
                .mapToInt(Integer::intValue)
                .toArray();
    }
}
